package com.qianchang.microfinance.view.credit.data;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.InputType;
import android.text.TextUtils;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bigkoo.pickerview.model.IPickerViewData;
import com.dm.library.utils.FormatUtil;
import com.qianchang.microfinance.R;
import com.qianchang.microfinance.bean.CommonSinglePickBean;
import com.qianchang.microfinance.bean.ContractBean;
import com.qianchang.microfinance.global.ConstantAppInfo;
import com.qianchang.microfinance.view.base.BaseActivity;
import com.qianchang.microfinance.view.dialog.CommonDialog;
import com.dm.library.adapter.TextWatcherAdapter;
import com.dm.library.bean.ContractListBean;
import com.dm.library.utils.ToastUtil;
import com.dm.library.widgets.OneLineEditLinearLayout;
import com.dm.library.widgets.OneLineLinearLayout;
import com.dm.library.widgets.element.DEditText;
import com.dm.library.widgets.element.DTextView;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 版 权: 深圳市迪蒙网络科技有限公司
 * 描 述:
 * 修 改 人: zhoulai
 * 修改时间: 2017/8/12 11:09
 */


public class AddContractActivity extends BaseActivity implements OneLineEditLinearLayout.TextWatcher, CommonDialog.OnItemSelectListener {
    //
    public static final String CONTRACT_BEAN = "contract_bean";
    private static final int REQUEST_CODE = 101;

    @BindView(R.id.et_contract_name)
    DEditText etContractName;
    @BindView(R.id.oll_phone)
    OneLineEditLinearLayout ollPhone;
    @BindView(R.id.oll_relationship)
    OneLineLinearLayout ollRelationship;
    @BindView(R.id.tv_get_contract)
    DTextView tvGetContract;
    @BindView(R.id.ll_content)
    LinearLayout llContent;
    @BindView(R.id.btn_commit_contract)
    TextView btnCommitContract;
    private ArrayList<Boolean> commitList;
    private String name = "";
    private String phone = "";
    private String relationshipId = "";
    private String relationship = "";
    private ContractBean contractBean;

    private int position;
    private int status;
    private ArrayList<CommonSinglePickBean> relationshipList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_contract);
        ButterKnife.bind(this);
        initView();
        initListener();
        initData();
    }

    @Override
    protected void initView() {
        super.initView();
        ollPhone.setInputType(InputType.TYPE_CLASS_NUMBER);
        ollPhone.setMaxLength(11);
    }


    private void initListener() {
        etContractName.addTextChangedListener(new TextWatcherAdapter() {
            @Override
            public void afterTextChanged(Editable s) {
                name = s.toString().trim();
//                isCommitAble();
            }
        });
        ollPhone.setTextWatcher(this, 0);
    }


    @Override
    protected void initData() {
        super.initData();
        relationshipList = BasicInitDataUtils.getRelationshipList(this);
        commitList = initCommitList(3);
        Bundle bundle = getBundle();
        if (null != bundle) {
            contractBean = (ContractBean) bundle.getSerializable(CONTRACT_BEAN);
            position = bundle.getInt(ConstantAppInfo.POSITION, -1);
            String title = bundle.getString(ConstantAppInfo.TITLE);
            setTitleName(title);
        }
        if (null == contractBean) {
            return;
        }
        name = contractBean.getcontactPeopleName();
        phone = contractBean.getcontactPeoplePhone();
        status = contractBean.getContactStatus();
        etContractName.setTextContent(name);
        ollPhone.setRightContent(phone);
//        MyDataStatus.setBtnStatus(status, btnCommitContract);
//        MyDataStatus.setContentEnable(status, llContent);
//        switch (status) {
////            case MyDataStatus.YRZ:
//            case MyDataStatus.YTJ:
//                tvGetContract.setVisibility(View.GONE);
//                break;
//            default:
//                tvGetContract.setVisibility(View.VISIBLE);
//                break;
//        }
        relationshipId = contractBean.getcontactRelationship();
        switch (relationshipId) {
            case "0":
                ollRelationship.setRightContent(getString(R.string.family));
                break;
            case "1":
                ollRelationship.setRightContent(getString(R.string.friend));
                break;
            case "2":
                ollRelationship.setRightContent(getString(R.string.colleague));
                break;
            default:
                ollRelationship.setRightContent(getString(R.string.please_select_contract));
                break;
        }
    }

    @Override
    public void afterTextChanged(Editable s, int flag) {
        phone = s.toString().trim();
    }

    private void isCommitAble() {
        commitList.set(0, !TextUtils.isEmpty(name));
        commitList.set(1, !TextUtils.isEmpty(phone));
        commitList.set(2, !TextUtils.isEmpty(relationshipId));
        btnCommitContract.setEnabled(checkAllCommitAble(commitList));
    }

    @OnClick({R.id.oll_relationship, R.id.tv_get_contract, R.id.btn_commit_contract})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.oll_relationship:
                CommonDialog.Builder builder = new CommonDialog.Builder();
                builder.setContext(this)
                        .setData(relationshipList)
                        .setTitle(getString(R.string.label_relationship))
                        .setOnItemSelectListener(this)
                        .build()
                        .showPickerView();
                break;
            case R.id.tv_get_contract:
                startActivityForResult(ContractListActivity.class, null, REQUEST_CODE);
                break;
            case R.id.btn_commit_contract:
//                if (null == phone || phone.length() != 11) {
                if(!FormatUtil.isValidName(name)){
                    ToastUtil.getInstant().show(this, "请输入正确的名字！");
                    return;
                }
                if (!FormatUtil.isValidPhone(phone, null)) {
                    ToastUtil.getInstant().show(this, "请输入正确的手机号！");
                    return;
                }
                if (TextUtils.isEmpty(relationshipId)) {
                    ToastUtil.getInstant().show(this, "请选择联系人类型！");
                    return;
                }

                Intent intent = getIntent();
                if (null != contractBean) {
                    contractBean.setcontactPeopleName(name);
                    contractBean.setcontactPeoplePhone(phone);
                    contractBean.setcontactRelationship(relationshipId);
                }
                intent.putExtra(CONTRACT_BEAN, contractBean);
                intent.putExtra(ConstantAppInfo.POSITION, position);
                setResult(RESULT_OK, intent);
                finish();
                break;
        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            if (null != data) {
                ContractListBean contractListBean = (ContractListBean) data.getSerializableExtra(ContractListActivity.CONTRACT_BEAN);
                if (null != contractListBean) {
                    name = contractListBean.getName();
                    phone = contractListBean.getPhone();
                    etContractName.setText(name);
                    ollPhone.setRightContent(phone);
                }
            }
        }
    }

    @Override
    public void onItemSelect(IPickerViewData data, int position, int flag) {
        CommonSinglePickBean commonSinglePickBean = relationshipList.get(position);
        relationshipId = commonSinglePickBean.getId();
        relationship = commonSinglePickBean.getName();
        ollRelationship.setRightContent(relationship);
    }
}
